<template>
	<view class="main">
		<view class="editUser-renzheng row-box" v-if="status == 0">
			<view class="editUser-line dis-clm poser" @click="realClick">
				<label>实名认证</label>
				<label>完成实名认证，找活找人更容易</label>
				<view class="renzheng-ico"><image src="http://xgr.oss-cn-hangzhou.aliyuncs.com/icon/images/my/rightJt.png"></image></view>
			</view>
		</view>
		<!--头像-->
		
		<view class="form-lst v-box">
			<view class="form-item noh1 poser" @click="unloads">
				<view class="itemimg-label">头像</view>
				<view class="item-imglst dis-row">
					<image class="icon" :src="FormData.avatar?FormData.avatar:'http://xgr.oss-cn-hangzhou.aliyuncs.com/icon/images/default-avatar.png'"></image>
				</view>
			</view>
			<view class="form-item noh2 poser">
				<view class="item-label">姓名<text style="color: #f00;">*</text></view>
				<view class="item-input">
					<input type="text" v-model="FormData.nickName" maxlength="10" class="input-sc" placeholder="请输入姓名"/>
				</view>
			</view>
			<view class="form-item noh2 poser">
				<view class="item-label">性别<text style="color: #f00;">*</text></view>
				<view class="item-input dis-row">
					<radio-group @change="radioChange" class="dis-row radiolst">
						<view class="rdio dis-row" v-for="(item, index) in sexRedo" :key="item.value">
							<view>
								<radio :value="item.value" :checked="item.value === current" />
							</view>
							<view>{{item.name}}</view>
						</view>
					</radio-group>
				</view>
			</view>
			<u--form :model="FormData" ref="form1" :label-style="{'font-size':'28rpx;'}">
				<u-form-item label="民族" labelWidth="100" @click="nationShow = true" borderBottom>
					<u--input v-model="FormData.nationName" inputAlign="right" border="none" fontSize="14" disabled="true" disabledColor="#ffffff"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				
				<u-form-item label="出生日期" labelWidth="100" @click="dateShow = true"  borderBottom>
					<u--input v-model="FormData.birthDate" inputAlign="right" border="none" fontSize="14" disabled="true" disabledColor="#ffffff"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
			</u--form>
		<!-- 	<view class="form-item noh2 poser">
				<view class="item-label">民族</view>
				<view class="item-input">
					<input type="text" class="input-sc" placeholder="请输入您的民族"/>
				</view>
			</view> -->
			<!-- <view class="form-item noh2 poser">
				<view class="item-label">出生日期</view>
				<view class="item-input dis-row">
					<view class="date-check">
						<gpp-date-picker @onCancel="onCancel" @onConfirm="onConfirm" :startDate="startDate" :endDate="endDate" :defaultValue="pickerDate">
						    <view class="gm dis-row">
								<view class="news">
									{{pickerDate}}
								</view>
								<view class="img-ico2"><image src="http://xgr.oss-cn-hangzhou.aliyuncs.com/icon/images/my/rightJt.png"></image></view>
							</view>
						</gpp-date-picker>
					</view>
					
				</view>
			</view> -->
			<view class="form-item noh2 poser">
				<view class="item-label">电话号码</view>
				<view class="item-input" @click="editPhone">
					<input type="text" readonly v-model="FormData.mobile" maxlength="11" class="input-sc" placeholder="请输入电话号码"/>
				</view>
			</view>
			
			<view class="send-button">
				<button type="primary" keyName="name" @click="sendPost">确定</button>
			</view>
			
			<u-picker :show="nationShow" ref="uPicker" :columns="nation"
			@cancel="noveOut"
			@confirm="nationConfirm"
			@change="nationChange"></u-picker>
			<u-datetime-picker
					:show="dateShow"
					v-model="value1"
					mode="date"
					:minDate='0'
					:maxDate="maxDate"
					@confirm="dateConfirm"
					@cancel="dateCancel"
					:formatter="formatter"
			></u-datetime-picker>
		</view>
		
		<pop ref="minzu" :direction="direction" :is_close="true" :is_mask="true" :width="100">
			<view class="new-del">
				 <view class="minzu-Name">选择民族</view>
				 <view class="minzu-lst" v-for="(n,v) in nation" :key="v">{{n.name}}</view>
			</view>
		</pop>
	</view>
</template>

<script>
import Index from "@/common/index-model.js"	
import pop from "@/components/ming-pop/ming-map.vue"
import fun from "@/common/publicFun.js"	
let index = new Index()
let that = null
export default {
	data() {
		return {
			direction:'below',
			sexRedo:[
				{name:'男',value:1},
				{name:'女',value:0},
			],
			nationShow: false,
			dateShow: false,
			nation: [],
			nationArr:[],
			FormData:{
				nickName:'',
				gender:1,
				nation:'',
				nationName:'',
				birthDate:'',
				mobile:'',
				avatar:''
			},
			current:1,
			value1:Number(new Date()),
			maxDate:0,
			status:0,
			prevPage:'',
		}
	},
	components:{
		pop
	},
	methods:{
		unloads() {
			//选择图片
			uni.chooseImage({
				count: 1,
				sizeType: ['original', 'compressed'], //original原图  compressed压缩
				sourceType: ['album', 'camera'], //album 相册  camera 相机
				success: (imgres) => {
					let resimg = imgres.tempFilePaths; //获取返回的文件数据
					//执行上传图片
					uni.uploadFile({
						url: index.img_url+'client/public/upload?type=headPicture&file='+resimg,
						filePath: resimg[0],
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							'X-Access-Token':uni.getStorageSync('token'),
							'Login-Type':'client'
						},
						success: (res) => {
						     //this.faceurl=JSON.parse(cmsres.data).data.imgurl
							let imgurl = JSON.parse(res.data).result.url
							this.FormData.avatar = imgurl
						},
						fail: (err) => {
							console.log(err);
						}
					})
				}
			}); //选择图片结束
		},
		nationChange(e) {
			// this.FormData.nation = this.nationArr[e.index].id
			// this.FormData.nationName  =this.nationArr[e.index].name
			// console.log(e,'======e======')
		},
		nationConfirm(e){
			// this.FormData.nation=this.nation[0][e.indexs].id;
			// this.FormData.nationName=this.nation[0][e.indexs].name;
			this.FormData.nation = this.nationArr[e.indexs[0]].id
			this.FormData.nationName  =this.nationArr[e.indexs[0]].name
			this.nationShow=false;
		},
		noveOut(e){
			this.nationShow=false;
			
		},
		dateConfirm(e){
			this.FormData.birthDate=fun.timestampToTime(e.value,'-');
			this.dateShow=false;
		},
		dateCancel(e){
			this.dateShow=false;
		},
		formatter(type, value) {
			if (type === 'year') {
				return `${value}年`
			}
			if (type === 'month') {
				return `${value}月`
			}
			if (type === 'day') {
				return `${value}日`
			}
			return value
		},
		radioChange(e) {
			for(let i = 0;i<this.sexRedo.length;i++){
				if(this.sexRedo[i].value == e.detail.value){
					this.current = this.sexRedo[i].value;
					this.FormData.gender=e.detail.value;
					break;
				}
			}
		},
		getNation(){
			index.getPublicUserMember({},'client/public/find/nationList','get',res=>{
				this.nationArr=res.data.result
				this.FormData.nation=this.nationArr[0].id;
				this.FormData.nationName=this.nationArr[0].name;
				let temp=[]
				for(let i=0;i<this.nationArr.length;i++){
					temp.push(this.nationArr[i].name)
				}
				this.nation.push(temp)
			})
		},
		sendPost(){
			uni.showLoading({
				title:'请稍后。。。',
			})
			if(!this.FormData.nickName){
				index.show_tips('请输入您的姓名');
				return false;
			}
			if(!this.FormData.birthDate){
				index.show_tips('请选择您的生日');
				return false;
			}
			if(!this.FormData.mobile){
				index.show_tips('请输入手机号码');
				return false;
			}else if(!index.is_phone_number(this.FormData.mobile)){
				index.show_tips('请输入正确的手机号码');
				return false;
			}
			let sends={
				"nickName":this.FormData.nickName,
				"gender":this.FormData.gender,
				"nation":this.FormData.nation,
				"birthDate":this.FormData.birthDate,
				"mobile":this.FormData.mobile,
				"avatar":this.FormData.avatar
			}
			index.getPublicUserMember(sends,'client/individual/information','post',res=>{
				if(res.data.code == 200 && res.data.success){
					this.getUserInfo();
					index.show_tips('个人信息修改成功')
					setTimeout(()=>{
						if(this.prevPage=='pages/index/index'){
							index.switch_tab('/'+this.prevPage)
						}else if(this.prevPage=='pages/worker/index'){
							index.switch_tab('/'+this.prevPage)
						}else if(this.prevPage=='pages/message/index'){
							index.switch_tab('/'+this.prevPage)
						}else if(this.prevPage=='pages/entertainment/index'){
							index.switch_tab('/'+this.prevPage)
						}else if(this.prevPage=='pages/user/index'){
							index.switch_tab('/'+this.prevPage)
						}else{
							uni.navigateBack();
						}
					},2000)
				}else{
					index.show_tips(res.data.message);
					return false
				}
			})
		},
		getUserInfo(){
			index.getPublicUserMember({},'client/individual/select/information','post',res=>{
				if(res.data.code == 200 && res.data.success){
					this.FormData.nickName=res.data.result.nickName;
					this.FormData.gender=res.data.result.gender?res.data.result.gender:1;
					if(res.data.result.mobile.charAt(0)==9){
						this.FormData.mobile='';
					}else{
						this.FormData.mobile=res.data.result.mobile;
					}
					this.FormData.nation=res.data.result.nation?res.data.result.nation:this.nationArr[0].id;
					this.FormData.nationName=res.data.result.nationStr?res.data.result.nationStr:this.nationArr[0].name;
					this.FormData.birthDate=res.data.result.birthDate;
					this.current=res.data.result.gender?res.data.result.gender:1;
					this.FormData.avatar=res.data.result.avatar;
					this.status=res.data.result.isRealName;
				}else{
					index.show_tips(res.data.message);
					return false
				}
			})
			
		},
		realClick() {
			index.navigate_to('./RealName')
		},
		editPhone(){
			index.navigate_to('./MyEditPhone')
		}
	},
	onLoad() {
		var pages    = getCurrentPages();
		let num=pages.length - 2<0?0:pages.length - 2;
		if(pages.length - 2<0){
			this.prevPage='pages/index/index';
		}else{
			var prevPage = pages[num].route;//来源页面
			this.prevPage=prevPage;
		}
		
		if(!uni.getStorageSync('token')){
			index.navigate_to('/pagesC/login/login')
		}
		this.getNation();
		this.getUserInfo();
		let date=new Date();
		this.maxDate=date.getTime();
	},
	onShow() {
		if(uni.getStorageSync("phone")){
			this.FormData.mobile=uni.getStorageSync("phone");
		}
	}
}
</script>

<style>
@import url("@/static/css/publics.css");
@import url("@/static/css/myedituserinfo.css");	
</style>